# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : __init__.py.py
# Time       ：2021/3/18 14:35
# Author     ：author name
# version    ：python 3.6
# Description：
"""

import requests
from bs4 import BeautifulSoup
import lxml
import json
from wangyiyun.config import HEADER
from wangyiyun.mysqlhelper import MysqldbHelper

db=MysqldbHelper()
base="http://zh.scorser.com/S/%e6%ad%8c%e8%af%8d/"


# url="http://zh.scorser.com/%e6%ad%8c%e8%af%8d/Neil+Young/Road+Rock+Vol+1/Walk+On/4678013.html"

url_end="/-1/1.html"

singer="Pink Floyd"
song="A Great Day for Freedom"

# http://zh.scorser.com/S/%E6%AD%8C%E8%AF%8D/Pink%20Floyd/-1/1.html
# http://zh.scorser.com/S/%E6%AD%8C%E8%AF%8D/Pink%20Floyd%20Any%20Colour%20You%20Like/-1/1.html
# http://zh.scorser.com/%e6%ad%8c%e8%af%8d/Pink+Floyd/The+Division+Bell/A+Great+Day+For+Freedom/4641384.html
from urllib.parse import urlencode
import urllib
import time

# print(r)

def insert(song_name,singer,lrc_text):
    sql='insert into song_sentiment(song_name,singer,lrc_text) values("%s","%s","%s")' %(song_name,singer,lrc_text)
    db.insert(sql)

def select():
    sql="select DISTINCT artname,traname from user_log where artname='Fleet Foxes'"
    r=db.selectAll(sql)
    song_info=set()
    for i in r:
        print(i)
        song_info.add((i[0],i[1]))

    for i in song_info:
        print(i)

    return song_info

def repeat(song_name,singer):
    sql="select count(*) from song_sentiment where song_name='%s' and singer='%s'" %(song_name,singer)
    r=db.selectAll(sql)
    print(r[0][0],len(r))
    if r[0][0]==1:
        return True
    else:
        return False

def get_lrc_text(singer,song):
    url = base + urllib.parse.quote(singer + " " + song + url_end)
    print(url)
    r = requests.get(url, headers=HEADER).text
    soup=BeautifulSoup(r,"lxml")
    r1=soup.find_all("div",class_="r")

    try:
        for i in r1:
            # print(i)
            r2=soup.find_all("a",class_="rmain")
            for j in r2:
                final_lrc = ''
                # print(j)
                print(j['href'])
                lrc_url=j['href']
                time.sleep(5)
                result=requests.get(lrc_url,headers=HEADER).text
                soup1=BeautifulSoup(result,"lxml")
                div1=soup1.find("div",class_="mainDiv")
                div2=div1.find_all('div')
                # print("div2:",len(div2),div2)
                lrc_div1=div2[1]
                # print("lrc_div1:", lrc_div1)
                lrc_div2 = lrc_div1.find_all('div')
                # print("lrc_div2:",lrc_div2)
                # print(len(lrc_div2))
                lrc_div3=lrc_div2[1]
                # print("lrc_div3:",lrc_div3)
                lrc_div4=lrc_div3.find_all('div')
                # print("lrc_div4:",lrc_div4)
                num=0

                pluso=lrc_div3.find('div',class_='pluso')
                pluso_text=pluso.get_text()
                for m in range(len(lrc_div4)):
                    con=lrc_div4[m]
                    # print("*******%s"%(m),lrc_div4[m])
                    if pluso==con.get_text():
                        num=m+1
                    # print("---------",num)
                # print(lrc_div4[6])
                # for k in lrc_div4:
                #     print(k)
                # print("get_text():",lrc_div4[4].get_text())
                # print("contents:",lrc_div4[4].contents)
                # print("text:",lrc_div4[4].text)

                # lrc_text=str(lrc_div4[5])
                #
                # l1=lrc_text.replace("<br/>"," ")
                # print(l1)
                lrc=[]
                for n in lrc_div4[4].contents:
                    if str(n)=="<br/>":
                        lrc.append(" ")
                    else:
                        lrc.append(str(n))

                lrc="".join(lrc)
                print(lrc)
                final_lrc=lrc
                insert(song_name=song, singer=singer, lrc_text=str(final_lrc))
                break
            break

    except Exception as e:
        print(e)
    # finally:
        # insert(song_name=song,singer=singer,lrc_text=final_lrc)
        final_lrc=''
data=select()
for i in data:
    print(i)
    is_repeat=repeat(singer=i[0], song_name=i[1])
    if is_repeat is True:
        continue

    get_lrc_text(singer=i[0],song=i[1])
    time.sleep(2)
    exit()
